package com.fenshi.manager.activity.backcar;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.fenshi.bean.OrderBean;
import com.fenshi.manager.R;
import com.fenshi.presenter.BackCarPresenter;
import com.fenshi.presenter.imp.BackCarPresenterImp;
import com.fenshi.view.BackView;
import com.quickdv.activity.BaseActivity;
import com.quickdv.until.AlertHelper;
import com.quickdv.until.LogHelper;

import butterknife.InjectView;


public class BackActivity extends BaseActivity implements View.OnClickListener, BackView {
    BackCarPresenter presenter;
    ProgressDialog loading;
    public static int BACKCAR = 301;
    public String path;
    int type = 0;

    @InjectView(R.id.order_code)
    TextView orderCode;
    @InjectView(R.id.order_user)
    TextView orderUser;
    @InjectView(R.id.order_type)
    TextView orderType;
    @InjectView(R.id.order_create_time)
    TextView orderCreateTime;
    @InjectView(R.id.order_back_car_time)
    TextView orderBackCarTime;
    @InjectView(R.id.order_car_distance)
    TextView orderCarDistance;
    @InjectView(R.id.order_consume_time)
    TextView orderConsumeTime;
    @InjectView(R.id.order_cost)
    TextView orderCost;
    @InjectView(R.id.car)
    TextView car;
    @InjectView(R.id.order_car_detail_linear)
    LinearLayout orderCarDetailLinear;
    @InjectView(R.id.back_car)
    Button backCar;
    @InjectView(R.id.car_status)
    TextView carStatus;
    AlertDialog dialog;
    @InjectView(R.id.order_mileagecost)
    TextView orderMileagecost;
    @InjectView(R.id.order_mileagecost_money)
    TextView orderMileagecostMoney;
    @InjectView(R.id.order_baseictariff)
    TextView orderBaseictariff;
    @InjectView(R.id.order_baseictariff_money)
    TextView orderBaseictariffMoney;
    @InjectView(R.id.order_cs_time_cost_money)
    TextView orderCsTimeCostMoney;
    @InjectView(R.id.order_limit_cost)
    TextView orderLimitCost;
    @InjectView(R.id.order_back_car_number)
    TextView orderBackCarNumber;

    @Override
    public int getLayoutID() {
        return R.layout.activity_back;
    }

    @Override
    public void initView() {
        AlertHelper helper = new AlertHelper(this);
        loading = helper.LoadingAlert();

        backCar.setOnClickListener(this);
        orderCarDetailLinear.setOnClickListener(this);
        presenter.backCar();

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提醒");
        builder.setMessage("车辆没有经过检验，是否确定退出");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                if (Build.VERSION.SDK_INT > 15)
                    finishAffinity();
                else
                    finish();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setCancelable(false);
        dialog = builder.create();
    }

    @Override
    public void initActionbar() {

    }

    @Override
    public void initData() {
        presenter = new BackCarPresenterImp(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.order_car_detail_linear:
                    presenter.upPic();
                break;

            case R.id.back_car:
                if (type == 0)
                    new AlertDialog.Builder(BackActivity.this).setTitle("还车确认").setMessage("确认当前汽车没有任何问题，可以还车").setPositiveButton(getString(R.string.sure), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int i) {
                            presenter.checkCar(type);
                        }
                    }).create().show();
                else
                    presenter.checkCar(type);
                break;
        }
    }

    @Override
    public void setBackLiearShow(int isShow) {

    }

    @Override
    public void setOrderDetailShow(int isShow) {

    }

    @Override
    public void setEumShow(int isShow) {
        carStatus.setVisibility(isShow);
    }

    @Override
    public void onBackPressed() {
        if (dialog != null && !dialog.isShowing())
            dialog.show();
    }

    @Override
    public void refreshView(OrderBean orderBean) {
        LogHelper.getInstance().e("order", JSON.toJSONString(orderBean));
        orderCode.setText(orderBean.getOrderCode());
        orderUser.setText(orderBean.getUserName());
        orderCreateTime.setText(orderBean.getCreateTime());
        orderBackCarTime.setText(orderBean.getEndTime());
        orderCarDistance.setText(orderBean.getDistance() + "");
        orderConsumeTime.setText(orderBean.getOrderTime());
        orderCost.setText("¥" +orderBean.getMoney() + "");
        if (orderBean.getStatus().equals("1") || orderBean.getStatus().equals("2")) {
            orderType.setText("订单使用中");
            orderType.setTextColor(getResources().getColor(R.color.red));
        }

        orderLimitCost.setText("¥" + orderBean.getExpense());
        orderBackCarNumber.setText(orderBean.getCarName());

        float time =Math.round(orderBean.getBasictariff()*100.0);
        orderBaseictariff.setText("x " + time/100.0 + "/分钟");
        float timeMoney =Math.round(orderBean.getOrderTimeMoney()*100.0);
        orderBaseictariffMoney.setText("金额:¥" + timeMoney/100.0);

        float mile =Math.round(orderBean.getMileagecost()*100);
        orderMileagecost.setText("x "+mile/100.0 + "/公里");
        float distanceMoney =Math.round(orderBean.getDistanceMoney()*100.0);
        orderMileagecostMoney.setText("金额:¥"+ distanceMoney/100.0);

        orderCsTimeCostMoney.setText("¥" + orderBean.getCsTimeMoney());
        orderType.setVisibility(View.VISIBLE);
    }

    @Override
    public void showLoad() {
        if (loading != null && !loading.isShowing()) loading.show();
    }

    @Override
    public void dismissLoad() {
        if (loading != null && loading.isShowing()) loading.dismiss();
    }

    @Override
    public void showError(String error) {
        Toast.makeText(this, error, Toast.LENGTH_LONG).show();
    }

    @Override
    public void initViewAndEvnet() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == BACKCAR) {
            if (resultCode == BACKCAR) {
                path = data.getExtras().getString("pic");
//                String dec = data.getExtras().getString("dec");
                type = 1;
                setEumShow(View.VISIBLE);
//                presenter.eum(path,dec);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


}
